package com.it.test.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.it.mapper.UserMapper;
import com.it.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * TODO Mapper层 查询方法
 */
@SpringBootTest
@Slf4j
public class selectText {

    @Autowired
    private UserMapper userMapper;


    /**
     * TODO selectById()：根据 id 查询一条记录
     */
    @Test
    public void selectById() {
        // 根据id查询
        User user = userMapper.selectById(1);
        System.out.println(user);
    }

    /**
     * TODO selectOne(QueryWrapper wrapper)
     *      根据queryWrapper条件，查询一条记录、如果多个，优先查询靠前的
     */
    @Test
    public void selectOne() {
        // 传入查询条件到实体类中
        User user = User.builder().phone("17799990014").build();
        // 将实体类条件传入到 QueryWrapper
        QueryWrapper<User> queryWrapper = new QueryWrapper<>(user);

        User res = userMapper.selectOne(queryWrapper);
        System.out.println("【查询结果】：" + res);
    }

    /**
     * TODO selectBatchIds(List ids)：根据 id 批量查询
     */
    @Test
    public void selectBatchIds() {
        List list = new ArrayList();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        List res = userMapper.selectBatchIds(list);
        System.out.println("【查询结果】：" + res);
    }

    /**
     * TODO selectList()：根据queryWrapper条件，查询多条记录
     */
    @Test
    public void selectList() {
        // 传入查询条件到实体类中,查询 gender为 2的
        User user = User.builder().gender(2).build();
        QueryWrapper<User> wrapper = new QueryWrapper<>(user);
        List<User> users = userMapper.selectList(wrapper);
        System.out.println("【查询结果】：" + users);
    }

    /**
     * TODO selectByMap(Map map)：根据 Map 条件，查询多条记录
     */
    @Test
    public void selectByMap() {
        Map m = new HashMap();
        m.put("gender", 1);
        List list = userMapper.selectByMap(m);
        System.out.println("【查询结果】：" + list);
    }

    /**
     * TODO selectByMaps(new QueryWrapper<>())：查询全部记录
     */
    @Test
    public void selectMaps() {
        List<Map<String, Object>> maps = userMapper.selectMaps(new QueryWrapper<>());
        System.out.println("【查询结果】：" + maps);
    }

}
